<template>
  <div class="subject">
    <span>{{ number1 }}</span>
    <span>+</span>
    <span>{{ number2 }}</span>
    <span>=</span>
    <input type="number" v-model="userAnswer" />
    <button @click="submitAnswer">提交</button>
  </div>
</template>

<script>
export default {
  data() {
    return {
      number1: this.getRandomNumber(),
      number2: this.getRandomNumber(),
      userAnswer: null
    };
  },
  methods: {
    getRandomNumber() {
      return Math.floor(Math.random() * 10) + 1;
    },
    submitAnswer() {
      let correctAnswer = this.number1 + this.number2;
      if (this.userAnswer == correctAnswer) {
        this.$emit('correct-answer');
      } else {
        this.$emit('wrong-answer');
      }
    //   this.userAnswer = null; // 清空输入
    }
  }
};
</script>


<style scoped>
.subject {
  margin: 5px;
  padding: 5px;
  font-size: 20px;
}
.subject span {
  display: inline-block;
  text-align: center;
  width: 20px;
}
.subject input {
  width: 50px;
  height: 20px;
}
</style>